package app

import (
	"fmt"
	"hft/Http/Services/front/app/app_model"
	"hft/Http/Services/front/app/app_res"
	utils "hft/Http/Utils"
	"hft/e"
	"hft/tools"
	"strconv"

	"github.com/gin-gonic/gin"
)

type Application struct{}

func (*Application) Index(c *gin.Context) {
	city, _ := strconv.Atoi(c.Request.Header.Get("sid"))
	if city == 0 {
		utils.FailWithMessage(e.NoManagerHere, c)
		return
	}
	if index, err := app_res.ApplicationService.Index(city); err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	} else {
		utils.OkWithData(index, c)
	}

}

func (*Application) TransSiteID(c *gin.Context) {
	city_name := c.Query("city_name")
	if city_name == "" {
		utils.FailWithMessage(e.RegionNotFound, c)
	} else {
		if city, err := app_res.ApplicationService.TransSiteID(city_name); err != nil {
			utils.FailWithMessage(err.Error(), c)
		} else {
			utils.OkWithData(city, c)
		}
	}
}

func (*Application) GetProvinceAndCity(c *gin.Context) {
	config := app_res.ApplicationService.GetProvinceAndCity()
	utils.OkWithData(config, c)
}

func (*Application) JoinUs(c *gin.Context) {
	var data app_model.JoinUsBody
	c.ShouldBindJSON(&data)
	fmt.Println("kkkk", tools.JsonParse(data))
	if err := app_res.ApplicationService.JoinUs(&data); err != nil {
		utils.FailWithMessage(err.Error(), c)
	} else {
		utils.Ok(c)
	}
}

func (*Application) GetAssistAndPrivateBody(c *gin.Context) {
	data := app_res.ApplicationService.GetAssistAndPrivate()
	utils.OkWithData(data, c)
}

var ApplicationController = new(Application)
